package com.highcom.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {
    protected static final Logger logger =   LoggerFactory.getLogger(RedisConfig.class);

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${spring.redis.jedis.pool.max-active}")
    private int maxTotal;

    @Value("${spring.redis.jedis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.jedis.pool.min-idle}")
    private int minIdle;


    // 创建Redis连接池

    public JedisPool redisPoolFactory(){
        // 创建Jedis连接池配置对象
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 设置最大连接数
        jedisPoolConfig.setMaxTotal(maxTotal);
        //设置最大空闲链接数
        jedisPoolConfig.setMaxIdle(maxIdle);
        // 设置最小空闲连接数
        jedisPoolConfig.setMaxIdle(minIdle);

        // 创建实例
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, null);
        //记录日志
        logger.info("JedisOPool注入成功~");
        logger.info("redis地址:" + host + ":" + port);
        //返回实例
        return jedisPool;
    }






}
